package handler

import (
	"2212b/2212ay1/api/request"
	"2212b/2212ay1/consts"
	"2212b/2212ay1/global"
	"2212b/2212ay1/internal/dao"
	"2212b/2212ay1/internal/model"
	"2212b/2212ay1/pkg"
	"context"
	"fmt"
	"github.com/dgrijalva/jwt-go"
	"github.com/gin-gonic/gin"
	jwt2 "github.com/golangblogs/gojwt"
	"math/rand"
	"net/http"
	"path/filepath"
	"strconv"
	"time"
)

func SendSms(c *gin.Context) {
	var req request.SendSmsReq

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "参数验证失败",
			"data": err.Error(),
		})
		return
	}
	//1手机号是否存在
	var user model.User
	dao.GetOneByFields(&model.User{
		Mobile: req.Mobile,
	}, &user)
	if user.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "用户不存在",
			"data": nil,
		})
		return
	}
	//2手机号短信发送是否成功60秒

	if global.RDB.Get(context.Background(), GetSendSmsKey(req.Mobile)).Val() != "" {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "短信验证码发送频率",
			"data": nil,
		})
		return
	}
	random := rand.Intn(900000) + 100000
	//3发送短信
	go pkg.SendSms(req.Mobile, strconv.Itoa(random))
	//4更新redis
	global.RDB.Set(context.Background(), GetSendSmsKey(req.Mobile), strconv.Itoa(random), time.Minute*1)
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "成功",
		"data": true,
	})
}

func GetSendSmsKey(mobile string) string {
	return fmt.Sprintf("mobile:%s", mobile)
}
func Register(c *gin.Context) {

}

func Login(c *gin.Context) {
	var req request.LoginReq

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "参数验证失败",
			"data": err.Error(),
		})
		return
	}
	//1手机号是否存在
	var user model.User
	dao.GetOneByFields(&model.User{
		Mobile: req.Mobile,
	}, &user)
	if user.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "用户不存在",
			"data": nil,
		})
		return
	}
	get := global.RDB.Get(context.Background(), GetSendSmsKey(req.Mobile))
	if get.Val() != req.SendSms {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "验证码错误",
			"data": nil,
		})
		return
	}
	token, err := jwt2.NewJWT(consts.JWT_KEY).CreateToken(jwt2.CustomClaims{
		ID: user.Id,
		StandardClaims: jwt.StandardClaims{
			ExpiresAt: time.Now().UnixMilli() + 3600*24*1,
		},
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "生成token失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "登录成功",
		"data": token,
	})
}

func GetUserToken(c *gin.Context) {
	token := c.Request.Header.Get("x-token")
	refreshToken, err := jwt2.NewJWT(consts.JWT_KEY).RefreshToken(token)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "更新token失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "更新token成功",
		"data": refreshToken,
	})
}

func GetUserList(c *gin.Context) {

}

func GetUserShow(c *gin.Context) {

}

func UploadFile(c *gin.Context) {
	file, _ := c.FormFile("file")
	ext := filepath.Ext(file.Filename)
	if ext != ".png" && ext != ".jpg" && ext != ".mp4" {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "上传的文件格式错误",
			"data": nil,
		})
		return
	}
	if file.Size < 1024*1024*30 {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "上传的文件大小不能超过30MB",
			"data": nil,
		})
		return
	}
	det := "D:\\phpstudy_pro\\WWW\\" + file.Filename
	c.SaveUploadedFile(file, det)

	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "更新token成功",
		"data": fmt.Sprintf("http://localhost/%s", file.Filename),
	})
}
